package com.timer;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Administrator on 2017/5/31.
 */
public class TimerDemo extends TimerTask {
    private static ConcurrentHashMap<Integer, Timer> concurrentHashMap = new ConcurrentHashMap<Integer, Timer>();
    private static CountDownLatch cdl = new CountDownLatch(5);
    private int id;

    public TimerDemo(int id) {
        this.id = id;
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(new R(i));
            thread.start();
            cdl.countDown();
        }
        //Thread.sleep(5000);
        //System.out.println(concurrentHashMap.size());
        System.out.println(1 << 2);
        //concurrentHashMap.get(2).cancel();
        //concurrentHashMap.get(2).purge();
    }

    @Override
    public void run() {
        try {
            cdl.await();
            System.out.println("id:" + id + "===" + new Date() + "==" + System.currentTimeMillis() + "===" + System.nanoTime());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class R implements Runnable {
        private int i;

        public R(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            Timer t = new Timer();
            TimerDemo timerDemo = new TimerDemo(i);
            t.schedule(timerDemo, 1000, 2000);
            concurrentHashMap.put(i, t);
        }
    }

}
